package com.ukao.pad.rfid;





import android.hardware.usb.UsbDevice;
import android.util.Log;

import com.ukao.pad.printer.CheckPrinterHelper;
import com.ukao.pad.usbrfidreader.UsbrfidReaderHelper;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;


/**
 * Created by herego on 2017/6/21.
 * 洗衣成员卡助手
 */

public class MembershipCardHelper {


    private int mUserIdStoreBlockIndex = 0x04; //16个字节
    private RfidHelper mRfidHelper;
    public String mCardId = "";
    public String mUserId = "";
    private static MembershipCardHelper mMembershipCardHelper;
    private UsbrfidReaderHelper mUsbrfidreaderHelper;




    public static MembershipCardHelper getInstance(){
        if(mMembershipCardHelper == null) {
            mMembershipCardHelper= new MembershipCardHelper();
        }
        return  mMembershipCardHelper;
    }

    private MembershipCardHelper() {

        if (CheckUtils.isMIUIDevices()){
            mUsbrfidreaderHelper= UsbrfidReaderHelper.getInstance();
        }else{
            mRfidHelper = RfidHelper.getInstance();
            mRfidHelper.openSerialPort();
        }

    }


    /**
     * 读用户id
     */
    public void readUserId(){
        if (CheckUtils.isMIUIDevices()){
            UsbDevice  mUsbDevice= CheckPrinterHelper.getConnectCardReader();
            mUsbrfidreaderHelper.readRfid(mUsbDevice);
        }else{
            mRfidHelper.readRfid(this.mUserIdStoreBlockIndex);
        }
    }

    /**
     * 读取会员卡
     */
    public void  readVipCard(){
        UsbDevice  mUsbDevice= CheckPrinterHelper.getConnectCardReader();
        if(mUsbDevice!=null){
            mUsbrfidreaderHelper=UsbrfidReaderHelper.getInstance();
            mUsbrfidreaderHelper.readRfid(mUsbDevice);
        }else {
            T.show("请连接Rfid");
        }
    }



    public void bindCard() {
        mRfidHelper.scanCard();
    }

    public void writeCard(String userId) {
        mRfidHelper.writeRfid(userId,mUserIdStoreBlockIndex);
    }






    public  void  onDestroy(){
        mMembershipCardHelper=null;
        if (mRfidHelper!=null){
            mRfidHelper.closeSerialPort();
            mRfidHelper=null;

        }
        if (mUsbrfidreaderHelper!=null){
            mUsbrfidreaderHelper=null;
        }

    }



}
